﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using XoLib.Tools;
using XoLib.Convertion;
using XoLib;
using XoLib.Compatibility;
//using System.Linq;
using System.Collections;
using XoLib.Serialization;

namespace XoLibCSharpText
{

    static class testaazaz
    {
        public static T test<T>(this ConfigXML source, string uri, string rr, T pp)
        {
            source.WriteXml(uri, rr, pp);
            return source.ReadXml<T>(uri, rr, default(T));
        }
    }


    public class A
    {
        public string s = "vache";
        public int i = 5;

        public bool Equals( A a)
        {
            
            return (s == a.s) && (i == a.i);
        }
    }


    [TestClass]
    public class UnitTest1
    {

        [TestMethod]
        public void XMLTest1()
        {
            A a = new A();
            a.i = 14;
             var r = CXmlString.SerializeToXml(a);
             A b = CXmlString.DeserializeFromXml<A>(r);

                 Assert.IsTrue(a.Equals(b));
        }

        [TestMethod]
        public void vache()
        {

            var ff = "145".ToOrDefault<int>();
            Assert.AreEqual(ff, 145);

             ff = "1f45".ToOrDefault<int>();
             Assert.AreEqual(ff, null);

             var fdf = "1.45".ToOrDefault<double>();
             Assert.AreEqual(fdf, 1.45);

        }





        [TestMethod]
        public void ConfigXmlTest2()
        {
            ConfigXML fff = new ConfigXML();
            ConfigXML fff2 = new ConfigXML();
            Assert.IsTrue(fff2.Load(null));

        }

        [TestMethod]
        public void ConfigXmlTest1()
        {

            ConfigXML fff = new ConfigXML();
            ConfigXML fff2 = new ConfigXML();

            int i = 8;
            double j = 3.5;
            string pp = "fjsdfhsdfhg";

            string ppi = "145";
            int ippi = 145;

            Assert.AreEqual(i, fff.test(@"Vache1",@"Testi",i));
            Assert.AreEqual(j, fff.test(@"Vache2", @"Testj", j));
            Assert.AreEqual(pp, fff.test(@"Vache3", @"Testpp", pp));
            Assert.AreEqual(pp, fff.test(null, @"Testpp", pp));
            Assert.AreEqual(pp, fff.test(@"Vache4", null, pp));

            Assert.AreEqual(ppi, fff.test(@"Vache8", null, ppi));

           

            Assert.IsTrue(fff.Save("c:\\ConfigXmlTest1.xml" ));
          
            Assert.IsTrue(fff2.Load("c:\\ConfigXmlTest1.xml" ));


            Assert.AreEqual(i, fff2.ReadXml(@"Vache1", @"Testi",-1));
            Assert.AreEqual(j, fff2.ReadXml<double>(@"Vache2", @"Testj",-1));
            Assert.AreEqual(pp, fff2.ReadXml<string>(@"Vache3", @"Testpp", null));
            Assert.AreEqual(pp, fff2.ReadXml<string>(null, @"Testpp", null));
            Assert.AreEqual(pp, fff2.ReadXml<string>(@"Vache4", null, null));

            Assert.AreEqual(ippi, fff2.ReadXml<int>(@"Vache8", null,-1));

            Assert.AreEqual(pp, fff2.ReadXml<int>(@"Vache4", null, -1));


           

        }

        [TestMethod]
        public void VersionTest1()
        {

            CVersion ver = new  CVersion();
            Assert.AreEqual(2, ver.FieldCount);

             ver = CVersion.GetInfo();
            CVersion ver2 = new CVersion(ver.ToString());

            Assert.AreEqual(ver, ver2);
                    
            Assert.AreEqual(2,new CVersion("1.2").FieldCount);
            Assert.AreEqual(3, new CVersion("1.2.3").FieldCount);
            Assert.AreEqual(4, new CVersion("1.2.3.4").FieldCount);
           
            ver = new CVersion("14.63.45");
            ver2 = new CVersion(ver.ToString());


            Assert.AreEqual("14", ver.ToString(1));
            Assert.AreEqual("14.63", ver.ToString(2));

            Assert.AreEqual(ver, ver2);

            ver = new CVersion("1.153");
            ver2 = new CVersion(ver.ToString());
            Assert.AreEqual(ver, ver2);

            Assert.AreEqual("1.153.0.0", ver.ToString(4));

            ver = new CVersion("189.5478");
            ver2 = new CVersion(ver.ToString());
            Assert.AreEqual(ver, ver2);



            string s = CVersion.GetInfo().DateVerbose();





            //  Assert.AreEqual("", s);

        }


        [TestMethod]
        public void HttpDownloadSTest()
        {
            HttpDownload tt = new HttpDownload();
            Assert.IsTrue(tt.DownloadFile("http://rml.lri.fr/papers/Mandel-These.pdf", "Z:\\Mandel-These.pdf"));
            
        }

        [TestMethod]
        public void HttpDownloadATest()
        {

            HttpDownload tt = new HttpDownload();
            tt.OnFinished += new HttpDownload.HttpDownloadEventHandler(HttpDownloadFinish);
            tt.DownloadFileAsync("http://rml.lri.fr/papers/Mandel-These.pdf", "Z:\\Mandel-These.pdf");

            
        }

        [TestMethod]
        public void HttpDownloadTest1()
        {

            HttpDownload.DownloadFileAsync("http://rml.lri.fr/papers/Mandel-These.pdf", "c:\\Mandel-These.pdf", HttpDownloadFinish); 

        }  

        public void HttpDownloadFinish(HttpDownload sender, long DataRead)
        {
            Console.Write(DataRead.ToString());
             Assert.AreNotEqual(-1,DataRead);
        }


  

        [TestMethod]
        public void Net20Test1()
        {
          
            /*
            string t ;
            t.Contains('r');
             * */
            Assert.AreEqual(true, NET20.Contains<char>(new char[5] { '1', '2', '3', '4', '5' }, '4'));
            Assert.AreEqual(false, NET20.Contains<char>(new char[5] { '1', '2', '3', '4', '5' }, '6'));

        }


        public class Crr
        {
            public string o;
            public string v;
            public bool bb;
        }

        

        [TestMethod]
        public void Net20Test2()
        {
           var gg = new Crr[5] { new Crr() { bb = true , o = "ee" }, new Crr() { bb = true, o = "ff"}, new Crr(), new Crr(), new Crr() };


          string hh = gg.Aggregate(p => p.o, p => p.bb);
          Assert.AreEqual("ee", hh);

            /*
            string t ;
            t.Contains('r');
             * */
            Assert.AreEqual(true, NET20.Contains<char>(new char[5] { '1', '2', '3', '4', '5' }, '4'));
            Assert.AreEqual(false, NET20.Contains<char>(new char[5] { '1', '2', '3', '4', '5' }, '6'));

        }  
       

        [TestMethod]
        public void LogTest1()
        {
            //int i = 1;
            CLogOnFile.WriteMessage("Test1").Version() /*.Entry(254, LogEntryType.Warning)*/ .Where().Write();

        }

        [TestMethod]
        public void LogTest5()
        {
            //int i = 1;
            CLogOnFile.WriteMessage("Test1").Where("Terer","eeeee", 0).Write();

        }

        [TestMethod]
        public void LogTest2()
        {
            int i = 2;
            CLogOnFile.WriteException(new Exception("Test2")).Variable("i", i).Write();

        }

        [TestMethod]
        public void LogTest3()
        {
            int i = 3;
            CLogOnFile.Open().Variable("i", i).Write();

        }


        [TestMethod]
        public void LogTest4()
        {

            try
            {
                string t = null;
                var i = t.Length;
            }
            catch (Exception ex)
            {
                CLogOnFile.WriteException(ex).Write();
            }

        }

    

          


    }
}
